package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTenantTitleMapper;
import com.ruoyi.system.domain.SysTenantTitle;
import com.ruoyi.system.service.ISysTenantTitleService;
import com.ruoyi.common.core.text.Convert;

/**
 * 租户顶部选项卡Service业务层处理
 * 
 * @author kuaidou
 * @date 2021-08-31
 */
@Service
public class SysTenantTitleServiceImpl implements ISysTenantTitleService 
{
    @Autowired
    private SysTenantTitleMapper sysTenantTitleMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询租户顶部选项卡
     * 
     * @param id 租户顶部选项卡主键
     * @return 租户顶部选项卡
     */
    @Override
    public SysTenantTitle selectSysTenantTitleById(Long id)
    {
        return sysTenantTitleMapper.selectSysTenantTitleById(id);
    }

    /**
     * 查询租户顶部选项卡列表
     * 
     * @param sysTenantTitle 租户顶部选项卡
     * @return 租户顶部选项卡
     */
    @Override
    public List<SysTenantTitle> selectSysTenantTitleList(SysTenantTitle sysTenantTitle)
    {
        sysTenantTitle.setTenantCode(ShiroUtils.getTenantCode());
        return sysTenantTitleMapper.selectSysTenantTitleList(sysTenantTitle);
    }

    /**
     * 新增租户顶部选项卡
     * 
     * @param sysTenantTitle 租户顶部选项卡
     * @return 结果
     */
    @Override
    public int insertSysTenantTitle(SysTenantTitle sysTenantTitle)
    {
        sysTenantTitle.setCreateTime(DateUtils.getNowDate());
        return sysTenantTitleMapper.insertSysTenantTitle(sysTenantTitle);
    }

    /**
     * 修改租户顶部选项卡
     * 
     * @param sysTenantTitle 租户顶部选项卡
     * @return 结果
     */
    @Override
    public int updateSysTenantTitle(SysTenantTitle sysTenantTitle)
    {
        sysTenantTitle.setUpdateTime(DateUtils.getNowDate());
        return sysTenantTitleMapper.updateSysTenantTitle(sysTenantTitle);
    }

    /**
     * 批量删除租户顶部选项卡
     * 
     * @param ids 需要删除的租户顶部选项卡主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantTitleByIds(String ids)
    {
        return sysTenantTitleMapper.deleteSysTenantTitleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除租户顶部选项卡信息
     * 
     * @param id 租户顶部选项卡主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantTitleById(Long id)
    {
        return sysTenantTitleMapper.deleteSysTenantTitleById(id);
    }

    /**
     * 查询用户顶部信息
     *
     * @param user
     * @return
     */
    @Override
    public List<SysTenantTitle> selectByUser(SysUser user) {
        List<SysRole> roles = sysRoleMapper.selectRolesByUserId(user.getUserId());
        StringBuilder roleSql = new StringBuilder("and(roles is null || roles=''");
        int count = 0;
        for (SysRole role : roles) {
            count++;
            if(count==1){
                roleSql.append(" || ");
            }
            roleSql.append("find_in_set('"+role.getRoleId()+"',roles)!=0");
            if (count != roles.size()){
                roleSql.append(" || ");
            }
        }
        roleSql.append(")");
        SysTenantTitle title=new SysTenantTitle();
        title.getParams().put("roleSql",roleSql.toString());
        title.setStatus(Constants.SUCCESS);
        title.setTenantCode(ShiroUtils.getTenantCode());
        return sysTenantTitleMapper.selectSysTenantTitleList(title);
    }
}
